package com.chl.jdk8.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import com.chl.jdk8.stream.Persion;

public class CompareSpeed {

	static List<Persion> persions;
	static List<Persion> persions1;

	@BeforeClass
	public static void init() {
		int size = 1000000;
		persions = new ArrayList<>(size);
		persions1 = new ArrayList<>(size);
		Persion p;
		Random r = new Random();
		for (int i = 0; i < size; i++) {
			p = new Persion();
			p.setAge(10 + r.nextInt(3));
			p.setGender(r.nextInt(10));
			p.setName("name" + i);
			persions.add(p);
			persions1.add(p);
		}
	}

	@Test
	public void lambda() {
		persions.sort((p1, p2) -> new CompareToBuilder().append(p1.getGender(), p2.getGender())
				.append(p1.getAge(), p2.getAge()).append(p1.getName(), p2.getName()).toComparison());
	}

	@Test
	public void innerClass() {
		persions.sort(new Comparator<Persion>() {
			@Override
			public int compare(Persion p1, Persion p2) {
				return new CompareToBuilder().append(p1.getGender(), p2.getGender()).append(p1.getAge(), p2.getAge())
						.append(p1.getName(), p2.getName()).toComparison();
			}

		});
	}

}
